//
//  UILabel+Exension.swift
//  Weibo
//
//  Created by xu on 2017/3/19.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit

extension UILabel {

    convenience init(title:String,fontSize:CGFloat,textColor:UIColor) {
      
        self.init()
        
        self.text = title
        self.font = UIFont.systemFont(ofSize: fontSize)
        self.textColor = textColor
        self.numberOfLines = 0;
        
        
    }

    

}


// 快速创建UIImageView
extension UIImageView{
    
    convenience init(imgName: String){
        self.init(image: UIImage(named: imgName))
    }
    
    
    //MARK: SD进行-进一步的封装
    
    func my_setImage(urlString: String?, placeholderImgName:String?){
        self.sd_setImage(with: URL(string: urlString ?? ""), placeholderImage: UIImage(named: placeholderImgName ?? ""))
        
    }
    
    
    
 
    
}

//图片上下文
extension UIImage {
// 获取主屏幕的内容图片
    
    class func getScreenSnap() -> UIImage?{
        // 获取主window
        let window = UIApplication.shared.keyWindow!
        // 开启上下文
        UIGraphicsBeginImageContext(window.bounds.size)
        // 将主window渲染到上下文中
        window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
        // 通过上下文获取对应的image
        let image = UIGraphicsGetImageFromCurrentImageContext()
        // 关闭上下文
        UIGraphicsEndImageContext()
        return image
    }
 
    
    
    // 对图片进行压缩处理
    func dealImageScale(maxWidth:CGFloat)->UIImage {
         // 判断图片的宽度小于指定的宽度 直接返回
        if self.size.width < maxWidth {
            return self
        }
        //计算出压缩的等比例高度
        let maxH = self.size.height * maxWidth / self.size.width
        
        let rect = CGRect(x: 0, y: 0, width: maxWidth, height: maxH)
        //开启图片上下文
        UIGraphicsBeginImageContext(rect.size)
        // 将image渲染到上下文中
        self.draw(in: rect)
       // 从上下文中获取image
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭图片上下文
        UIGraphicsEndImageContext()
        
        return image!

    }
    
}


